package com.heer.gymnasium.activity;


import android.widget.AbsListView;
import android.widget.ListView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.heer.api.ResponseData.ApiResponse;
import com.heer.gymnasium.R;
import com.heer.gymnasium.adapter.MessageAdapter;
import com.heer.model.MyMsgBean;



import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/8/2.
 */

public class MyMessageActivity extends BaseRefreshActivity {

    @BindView(R.id.list)
    ListView list;

    SVProgressHUD svProgressHUD;

    MessageAdapter adapter;
    List<MyMsgBean> data=new ArrayList<>();

    int page=1;
    boolean laod=true;

    @Override
    protected void initView() {
        super.initView();
         svProgressHUD = new SVProgressHUD(mContext);
        adapter=new MessageAdapter(this);
        list.setAdapter(adapter);
        titleBuilder.setTitleText("我的消息");
        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView,int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                if (laod && absListView.getLastVisiblePosition() == totalItemCount - 1 && absListView.getLastVisiblePosition()>0) {
                    laod=false;
                    pageNo++;
                    loadData(true);

                }

            }
        });
    }

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.activity_my_message);

    }


    @Override
    protected void loadData(boolean loadmore) {
        super.loadData(loadmore);
        svProgressHUD.show();
        mAppHelper.apiService().getMyMsgList("20",pageNo+"").enqueue(new Callback<ApiResponse<List<MyMsgBean>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<MyMsgBean>>> call, Response<ApiResponse<List<MyMsgBean>>> response) {
                svProgressHUD.dismiss();
                layoutRefresh.setRefreshing(false);
                if (response.isSuccessful() && response.body().isSccessful())
                {

                    if (response.code()==200)
                    {
                        if (pageNo==1)
                        {
                            adapter.setItems(response.body().getData());
                        }else {
                            adapter.addItems(response.body().getData());
                        }
                        if (response.body().getData().size()==20)
                        {
                            laod=true;
                        }

                    }
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<MyMsgBean>>> call, Throwable t) {
                svProgressHUD.dismiss();
                layoutRefresh.setRefreshing(false);
            }
        });
    }




}
